package com.wink.cachedemo.controller;


import com.wink.cachedemo.cache.RedisService;
import com.wink.cachedemo.common.ApiResponse;
import com.wink.cachedemo.entity.User;
import com.wink.cachedemo.service.UserServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * cache test controller
 *
 * @author xw
 * @date 2020/10/27 13:48
 */
@RestController
@RequestMapping("/user")
@AllArgsConstructor
public class UserController {

    private final UserServiceImpl userService;

    private final RedisService redisService;

    @GetMapping("/getList")
    public ApiResponse<List<User>> getList() {
        return ApiResponse.ok(userService.getList());
    }

    @GetMapping("/getOne")
    public ApiResponse<User> getOne(Long id) {
        return ApiResponse.ok(userService.getOne(id));
    }

    @PostMapping("/addUser")
    public ApiResponse<Boolean> addUser(User user) {
        return ApiResponse.ok(userService.addUser(user));
    }

    @PostMapping("/deleteUser")
    public ApiResponse<Boolean> deleteUser(Long id) {
        return ApiResponse.ok(userService.deleteUser(id));
    }

    @PostMapping("/updateUser")
    public ApiResponse<Boolean> updateUser(Long id,String password) {
        return ApiResponse.ok(userService.updateUser(id,password));
    }

    @PostMapping("/testRedisMsg")
    public ApiResponse<Void> testRedisMsg(String msg) {
        redisService.sendMessage("abc",msg+"--->abc");
        redisService.sendMessage("efg",msg+"--->efg");
        redisService.sendMessage("ccccc",msg+"--->ccccc");
        return ApiResponse.ok();
    }
}
